# coding: utf-8
from sanic.response import html
from jinja2 import Environment, FileSystemLoader, select_autoescape
from functools import wraps

env = Environment(loader = FileSystemLoader('templates'), autoescape = select_autoescape(['html', 'xml']), enable_async=True)

# 渲染html模版
class RenderHtml:
    @staticmethod
    def template(template_name):
        def warapper(func):
            @wraps(func)
            async def inner(request, *args, **kwargs):
                template = env.get_template(template_name)
                context =await func(request, *args, **kwargs)
                return html(await template.render_async(context))
            return inner
        return warapper